//
//  YCGetBackPasswordViewController.m
//  test
//
//  Created by 大锅 on 2017/7/13.
//  Copyright © 2017年 daguo. All rights reserved.
//

#import "YCGetBackPasswordViewController.h"
#import "YCLoginManager.h"
#import "CheckVerifiCodeViewController.h"

@interface YCGetBackPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;


@end

@implementation YCGetBackPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回登录密码";
    [_phoneNum becomeFirstResponder];
    [_phoneNum addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
    
    _phoneNum.text = [[YCLoginManager shareManager].phoneNumber telephoneNumberFormat];
}

- (IBAction)nextClick:(id)sender {
    
    [self.view endEditing:YES];
    
    if (![YCCommonTool validatePhoneNumber:[_phoneNum.text cleanTelephoneNumber]]) {
        
        [self presentFailureTips:@"手机号格式不对"];
        return ;
    }
    
    [self presentLoadingTips:@""];
    
    [[YCLoginManager shareManager] accountIsExistPhoneNumber:[_phoneNum.text cleanTelephoneNumber] complete:^(BOOL success, NSString *message) {
        
        [self dismissTips];
        if (success) {
            
            CheckVerifiCodeViewController *loginVerifyVC = [[CheckVerifiCodeViewController alloc]init];
            loginVerifyVC.typeFrom = TypeFromGetBackPwd;
            loginVerifyVC.isGetCodeAgain = YES;
            loginVerifyVC.phoneStr = _phoneNum.text;
            [self showViewController:loginVerifyVC sender:nil];
            
        }else{
            
            [self dismissTips];
            [self presentFailureTips:message];
        }
    }];
}

/**
 *  当输入手机号码变化
 *
 *  @param textfied
 */
#pragma mark - 手机号码输入11位时自动跳转
-(void)textChanged:(UITextField *)textfied {
    textfied.text = [textfied.text telephoneNumberFormat];
    if (textfied.text.length >= 13) {
        
        textfied.text = [textfied.text substringWithRange:NSMakeRange(0, 13)];
        
        [textfied resignFirstResponder];
    }
}

- (void)dealloc {
    [self clearKeyboardCorverNotificationAndGesture];
    [self tapGestureHandel];
}


@end
